package mongodb;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import static configure.OutputConfig.*;

public class OutputData {
    public static void main(String[] args) throws Exception {
        LinkedList<Long> list = new LinkedList<>();
        LinkedList<Long> list2 = new LinkedList<>();
        list.add(new Long(1477));
        list.add(new Long(1476));
        list2.add(new Long(1336));
        list2.add(new Long(1335));
        mongoDataOut(list, list2);
    }

    public static void mongoDataOut() throws Exception {
        URL url = new URL(GET_URL_CASE);
        String case_result = getDocument(url);
        url = new URL(GET_URL_TARGET);
        String target_result = getDocument(url);
        //JSONObject jsonObject = JSONObject.fromObject(case_result);
        //JSONArray array = JSONArray.fromObject(jsonObject.get("_embedded"));
        //System.out.println(array.get(0));
        //jsonObject.put("_embedded","123");
        //System.out.println(jsonObject.toString());
        try {
            outputFile(case_result, CASE_OUTPUT_FILEPATH, CASE_OUTPUT_FILE_NAME);
            outputFile(target_result, TARGET_OUTPUT_FILEPATH, TARGET_OUTPUT_FILE_NAME);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public static void mongoDataOut(List<Long> caseIds, List<Long> targetIds) throws Exception {
        LinkedList<Object> caseList = new LinkedList<>();
        LinkedList<Object> targetList = new LinkedList<>();
        for(int i=0;i<caseIds.size();i++){
            URL getCaseUrl = new URL(GET_URL_CASE + "?filter={'caseId':" + caseIds.get(i) + "}");
            String case_result = getDocument(getCaseUrl);
            JSONObject caseResultJsonObject = JSONObject.fromObject(case_result);
            try {
                JSONArray caseArray = JSONArray.fromObject(caseResultJsonObject.get("_embedded"));
                caseList.add(caseArray.get(0));
            }catch (Exception e){
                System.out.println("找不到caseId为"+targetIds.get(i)+"的数据");
            }
        }

        for (int i=0;i<targetIds.size();i++){
            URL getTargetUrl = new URL(GET_URL_TARGET + "?filter={'targetId':" + targetIds.get(i) + "}");
            String target_result = getDocument(getTargetUrl);
            JSONObject targetResultJsonObject = JSONObject.fromObject(target_result);
            try {
                JSONArray targetArray = JSONArray.fromObject(targetResultJsonObject.get("_embedded"));
                targetList.add(targetArray.get(0));
            }catch (Exception e){
                System.out.println("找不到targetId为"+targetIds.get(i)+"的数据");
            }

        }
        outputFile(caseList,CASE_OUTPUT_FILEPATH,CASE_OUTPUT_FILE_NAME);
        outputFile(targetList,TARGET_OUTPUT_FILEPATH,TARGET_OUTPUT_FILE_NAME);
    }

    public static String getDocument(URL url) {

        String json_result = "";
        try {
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InetAddress address = InetAddress.getLocalHost();
            String ip = address.getHostAddress();//获得本机IP
            connection.setRequestProperty("ip", ip);  //请求来源IP
            connection.setRequestProperty("Authorization", Authorization);
            connection.connect();

            // 获取输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {// 循环读取流
                sb.append(line);
                //System.out.println(br.toString());
            }
            br.close();// 关闭流
            connection.disconnect();// 断开连接
            //System.out.println(sb.toString());
            json_result = sb.toString();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return json_result;
    }

    public static void outputFile(Object outputObject, String filepath, String fileName) throws IOException {
        File file = new File(filepath + fileName);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(file));
        objOut.writeObject(outputObject);
        objOut.close();
    }
}
/*
//处理case
        URL getCaseUrl = new URL(GET_URL_CASE + "?filter={'caseId':" + caseIds.get(0) + "}");
        //获取case数组
        String case_result = getDocument(getCaseUrl);
        JSONObject caseResultJsonObject = JSONObject.fromObject(case_result);
        JSONArray caseArray = JSONArray.fromObject(caseResultJsonObject.get("_embedded"));
        for (int i = 1; i < caseIds.size(); i++) {
            getCaseUrl = new URL(GET_URL_CASE + "?filter={'caseId':" + caseIds.get(i) + "}");
            String tempStr = getDocument(getCaseUrl);
            JSONObject tempJsonObject = JSONObject.fromObject(tempStr);
            JSONObject tempCase = JSONArray.fromObject(tempJsonObject.get("_embedded")).getJSONObject(0);
            caseArray.add(tempCase);
        }
        caseResultJsonObject.put("_embedded", caseArray);
        //System.out.println(caseResultJsonObject.toString());
        //处理target
        URL getTargetUrl = new URL(GET_URL_TARGET + "?filter={'targetId':" + targetIds.get(0) + "}");
        String target_result = getDocument(getTargetUrl);
        JSONObject targetResultJsonObject = JSONObject.fromObject(target_result);
        JSONArray targetArray = JSONArray.fromObject(targetResultJsonObject.get("_embedded"));
        //System.out.println(targetIds.size());
        for (int i = 0; i < targetIds.size(); i++) {
            getTargetUrl = new URL(GET_URL_TARGET + "?filter={'targetId':" + targetIds.get(i) + "}");

            String tempStr = getDocument(getTargetUrl);
            //System.out.println(tempStr==null);
            JSONObject tempJsonObject = JSONObject.fromObject(tempStr);
            //System.out.println(tempJsonObject.toString());
            //System.out.println(i+":"+targetIds.get(i)+":"+tempJsonObject.get("_embedded").toString());
            try {
                JSONObject tempTarget = JSONArray.fromObject(tempJsonObject.get("_embedded")).getJSONObject(0);
                targetArray.add(tempTarget);
            } catch (Exception e) {
                System.out.println("mongodb中找不到targetId=" + targetIds.get(i) + "的数据");
            }

        }
        targetResultJsonObject.put("_embedded", targetArray);
        //System.out.println(targetResultJsonObject.toString());
        try {
            outputFile(caseResultJsonObject.toString(), CASE_OUTPUT_FILEPATH, CASE_OUTPUT_FILE_NAME);
            outputFile(targetResultJsonObject.toString(), TARGET_OUTPUT_FILEPATH, TARGET_OUTPUT_FILE_NAME);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }





*/